package com.hmkj.web.controller.index;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.constant.RiskNID;
import com.hmkj.core.model.order.CommoModel;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.operate.Agent;
import com.hmkj.core.po.operate.Banner;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.operate.BannerService;
import com.hmkj.core.service.risk.RiskParamService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(description = "首页相关")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL)
public class IndexController extends GenericController<Integer, User> {

    @Resource
    private BannerService bannerService;
    @Resource
    private RiskParamService riskParamService;
    @Resource
    private AgentService agentService;

    @ApiOperation(value = "Banner", notes = "Banner")
    @RequestMapping(value = "banner", method = RequestMethod.POST)
    public ResultEntity<List<Banner>> banner() throws Exception {
        log.info("获取banner列表");
        String domain = getDomain();
        Agent agent = agentService.selectAgentByDomain(domain);
        List<Banner> bannerList = bannerService.findBannerList(agent);
        return ok(bannerList);
    }

    @ApiOperation(value = "资讯", notes = "资讯")
    @RequestMapping(value = "news", method = RequestMethod.POST)
    public ResultEntity<List<String>> news() throws Exception {
        log.info("获取缓存资讯");
        return ok(redisService.getList(CacheID.NEWS, String.class));
    }

    @ApiOperation(value = "期货列表", notes = "期货列表")
    @RequestMapping(value = "futures", method = RequestMethod.POST)
    public ResultEntity<List<CommoModel>> futures() throws Exception {
        log.info("获取期货列表");
        List<CommoModel> inlist = new ArrayList<>();
        List<CommoModel> outlist = new ArrayList<>();
        List<Commo> list = redisService.getList(CacheID.COMMO_LIST, Commo.class);
        list.stream().forEach(commo -> {
            CommoModel model = new CommoModel();
            BeanUtils.copyProperties(commo, model);
            model.setContract(riskParamService.getContractNo(commo.getId()));
            model.setWavePointMin(riskParamService.getParamValue(RiskNID.WAVE_POINT_MIN,commo.getId(), BigDecimal.class));
            if (commo.getType().intValue() == Commo.TYPE.T1.code){
                outlist.add(model);
            }else {
                inlist.add(model);
            }
        });
        return ok(m->{
            m.put("inlist",inlist);
            m.put("outlist",outlist);
        });
    }

    @ApiOperation(value = "审核版本开关", notes = "审核版本开关")
    @RequestMapping(value = "isAudit", method = RequestMethod.POST)
    public ResultEntity isAudit() throws Exception {
        log.info("审核版本开关");
        return ok(m -> m.put("isAudit", redisService.get(ConfigNID.IOS_ISAUDIT, Boolean.class)));
    }
}
